package com.devbobcorn.misaka.entities;

import com.devbobcorn.misaka.items.ModItems;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.projectile.ProjectileItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.IPacket;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ItemParticleData;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkHooks;


public class CoinEntity extends ProjectileItemEntity {
    public CoinEntity(EntityType<? extends CoinEntity> type, World world) {
        super(type, world);
    }

    public CoinEntity(World world, LivingEntity player) {
        super(ModEntityType.COIN_TYPE, player, world);
    }

    public CoinEntity(World world, double x, double y, double z) {
        super(ModEntityType.COIN_TYPE, x, y, z, world);
    }

    protected Item getDefaultItem() {
        return ModItems.COIN.get();
    }

    @OnlyIn(Dist.CLIENT)
    private IParticleData getParticle() {
       ItemStack itemstack = this.getItemRaw();
       return (IParticleData)(itemstack.isEmpty() ? ParticleTypes.ITEM_SNOWBALL : new ItemParticleData(ParticleTypes.ITEM, itemstack));
    }

    public void handleEntityEvent(byte flags) {
        if (flags == 3) {
            IParticleData iparticledata = this.getParticle();
            
            for (int i = 0; i < 8; ++i) {
                this.level.addParticle(iparticledata, this.getX(), this.getY(), this.getZ(), 0.0D, 0.0D, 0.0D);
            }
        }
    }

    @SuppressWarnings({ "null" })
    protected void onHitEntity(EntityRayTraceResult hit) {
        super.onHitEntity(hit);

        Entity entity = hit.getEntity();
        Entity thrower = this.getOwner();

        // Apply base damage
        entity.hurt(DamageSource.thrown(this, thrower), 2.0F);

        // Summon a lightning bolt if possible, and play hit sound
        // See ThrownTrident.class
        SoundEvent soundevent = SoundEvents.TRIDENT_HIT;
        float vol = 1.0F;
        if (this.level instanceof ServerWorld) {
            BlockPos blockpos = entity.blockPosition();
            if (this.level.canSeeSky(blockpos)) {
                LightningBoltEntity lightningbolt = EntityType.LIGHTNING_BOLT.create(this.level);
                lightningbolt.moveTo(Vector3d.atBottomCenterOf(blockpos));
                lightningbolt.setCause(thrower instanceof ServerPlayerEntity ? (ServerPlayerEntity) thrower : null);
                this.level.addFreshEntity(lightningbolt);
                soundevent = SoundEvents.TRIDENT_THUNDER;
                vol = 3.0F;
            }
        }

        this.playSound(soundevent, vol, 1.0F);

    }

    protected void onHit(RayTraceResult hit) {
        super.onHit(hit);
        if (!this.level.isClientSide) {
            this.level.broadcastEntityEvent(this, (byte) 3);
            this.remove();
        }
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }

}